﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;

namespace AutoPageHelper
{
    class DataManager : IDataSource
    {
        private DataTable dataTable = new DataTable();
        private int curIndex = -1;
        #region IDataSource<DataRow> 成员

        public bool Load(string strSource, string strColumns)
        {
            if (string.IsNullOrEmpty(strSource) || string.IsNullOrEmpty(strColumns))
                return false;
            DataSet dsData = new DataSet();
            using (OleDbConnection conn = new OleDbConnection(strSource))
            {
                conn.Open();
                DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                foreach (DataRow schemaRow in schemaTable.Rows)
                {
                    string sheet = schemaRow["TABLE_NAME"].ToString();
                    if (!sheet.EndsWith("_"))
                    {
                        OleDbCommand cmd = new OleDbCommand("SELECT " + strColumns + " FROM [" + sheet + "]", conn);
                        cmd.CommandType = CommandType.Text;
                        OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
                        try
                        {
                            adp.Fill(dsData, sheet);
                            if (dsData.Tables[sheet].Columns.Count > 0 && dsData.Tables[sheet].Rows.Count > 0)
                                break;
                            else
                                dsData.Tables.Remove(sheet);
                        }
                        catch(OleDbException)
                        {
                            // column not match
                            Debug.WriteLine("Invalid datasheet : " + sheet);
                        }
                    }
                }
            }
            if (dsData.Tables.Count > 0)
            {
                dataTable = dsData.Tables[0].Copy();
                return true;
            }
            else
                return false;
        }

        public object GetData()
        {
            if (curIndex < 0)
                return null;
            Dictionary<string, string> data = new Dictionary<string, string>();
            DataRow dr = dataTable.Rows[curIndex];
            foreach (DataColumn col in dataTable.Columns)
            {
                data.Add(col.ColumnName, dr[col].ToString());
            }
            return data;
        }

        public bool MoveNext()
        {
            curIndex++;
            if (curIndex >= dataTable.Rows.Count)
            {
                curIndex = - 1;
                return false;
            }
            return true;
        }

        public bool MovePrevious()
        {
            curIndex--;
            if (curIndex < 0)
            {
                return false;
            }
            return true;
        }

        public void MoveFirst()
        {
            curIndex = 0;
        }

        public void MoveLast()
        {
            curIndex = dataTable.Rows.Count - 1;
        }

        #endregion
    }
}
